package user_assets

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserBankInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户银行卡信息 - 查询
func NewUserBankInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserBankInfoLogic {
	return &UserBankInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserBankInfoLogic) UserBankInfo(req *types.UserBankInfoRequest) (resp *types.UserBankInfoResponse, err error) {
	info, err := l.svcCtx.AssetsRpc.UserChannel.UserBankInfo(l.ctx, &rpc.UserBankInfoRequest{
		UserId:     req.UserId,
		Username:   req.UserName,
		CardNumber: req.CardNum,
		Email:      req.Email,
		Phone:      req.Phone,
		BankName:   req.BankName,
		Province:   req.Province,
		City:       req.City,
		BranchBank: req.BranchBank,
		BankId:     req.BankId,
		AgentId:    req.AgentId,
	})
	var infos []*types.UserBankInfo

	if err != nil {
		return &types.UserBankInfoResponse{
			UserBankInfo: infos,
		}, err
	}
	for _, bankInfo := range info.GetUserBankInfo() {
		infos = append(infos, &types.UserBankInfo{
			UserId:     bankInfo.UserId,
			UserName:   bankInfo.UserName,
			CardNum:    bankInfo.CardNumber,
			Email:      bankInfo.Email,
			Phone:      bankInfo.Phone,
			BankName:   bankInfo.BankName,
			Province:   bankInfo.Province,
			City:       bankInfo.City,
			BranchBank: bankInfo.BranchBank,
			BankId:     bankInfo.BankId,
			AgentId:    bankInfo.AgentId,
		})
	}

	return &types.UserBankInfoResponse{
		UserBankInfo: infos,
	}, nil
}
